-- UIGainPetSnapshot
-- Create by chengb Jan/5/2015
-- 获得冈布奥屏幕快照

-- UIGainPetSnapshot继承自Layer
UIGainPetSnapshot = class("UIGainPetSnapshot", function()
    return cc.Layer:create();
end);

function UIGainPetSnapshot.create(classId)
    return UIGainPetSnapshot.new(classId);
end

-- 构造函数
function UIGainPetSnapshot:ctor(classId)
    -- 初始化
    self:setName("UIGainPetSnapshot");
    local node = cc.CSLoader:createNode("layout/snapshot/GainPetSnapshot.csb");
    self:addChild(node);
    self.node = node;

    self.classId = classId;

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIGainPetSnapshot:resize()
    local node = self.node;
    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;

    -- bg居中显示并充满整个屏幕
    AlignM.alignToCenter(node, "bg");

    local heroBg = findChildByName(node, "bg/hero_bg");
    if h / w < 1.5 then
        heroBg:setScaleX(768 / DESIGN_WIDTH);
    end

    -- 二维码图片缩放
    local scale = math.min(w / DESIGN_WIDTH, h / DESIGN_HEIGHT);
    local codeImg = findChildByName(node, "qr_code");
    codeImg:setScale(scale);
    codeImg:setPosition(w / 2, 40 * scale);

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- TODO 限制界面大小不超过参考尺寸
    local curSize = math.max(w, h);
    if curSize > SHARE_IMAGE_REF_SIZE then
        local scale = SHARE_IMAGE_REF_SIZE / curSize;
        node:setScale(scale);
    end
end

-- 获取界面的缩放比例
function UIGainPetSnapshot:getNodeScale()
    return self.node:getScale();
end

-- 重绘
function UIGainPetSnapshot:redraw()
    local CT = self.node:getChildByName("CT");

    -- 标题
    local titleImg = findChildByName(CT, "title_icon");
    titleImg:loadTexture("images/logo/cn_game_name.png");
    titleImg:setScale(1.1);

    -- 显示冈布奥名字
    local nameLabel = findChildByName(CT, "name");
    local petInfo = PetM.query(self.classId);
    local name = PetM.query(self.classId, "name");
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_LARGE2, TextStyleM.TEXT_COLOR_ORANGE, true, -2, nil, nil, nil, nil, true);
    nameLabel:setString(name);

    -- 显示专属技描述
    local descLabel  = findChildByName(CT, "desc");
    if not isOverseaPackage() then
        -- 国内平台不显示描述，需要显示二维码
        descLabel:setVisible(false);
    else
        descLabel:setVisible(true);
        local skillId = petInfo["special"];
        local desc = SkillM.getSkillDesc(skillId, false, "share");
        desc = extractString(desc);
        TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BRIGHT_YELLOW);
        descLabel:setString(desc);
    end

    -- 创建冈布奥模型
    self:createHeroModel();

    -- 重绘招募部分信息
    self:redrawPetRecruitNode();

    -- 显示二维码
    self:redrawQRCode();
end

-- 重绘招募部分信息
function UIGainPetSnapshot:redrawPetRecruitNode()
    -- 获取阵营信息
    local campId = CampM.getPetCamp(self.classId);

    -- 背景
    local heroBg = findChildByName(self.node, "bg/hero_bg");
    local campIconPath = getHeroBgIconPath(campId);
    heroBg:loadTexture(campIconPath);

    -- 效果
    local effectImg = findChildByName(self.node, "bg/effect_img");
    effectImg:loadTexture(getMercenaryCampIconPath("effect" .. campId));
    effectImg:stopAllActions();
    effectImg:removeAllChildren();
    local effectList = {1287, 1286, 1288, 1289};
    playEffect(effectImg, effectList[campId], 300, 250, nil, 2.35);
end

-- 创建英雄模型
function UIGainPetSnapshot:createHeroModel()
    local modelNode = findChildByName(self.node, "CT/model_node");
    local headImg   = findChildByName(self.node, "CT/hero_icon");
    modelNode:removeAllChildren();
    local modelId = PetM.query(self.classId, "default_model");
    createHeroModel(modelId, headImg, modelNode, 1.0);
end

-- 显示二维码
function UIGainPetSnapshot:redrawQRCode()
    local QRCodeImg = findChildByName(self.node, "qr_code");

    -- 国内平台，显示二维码
    if not isOverseaPackage() then
        QRCodeImg:setVisible(true);
        QRCodeImg:loadTexture("images/logo/lt_qr_code.png");
    else
        QRCodeImg:setVisible(false);
    end
end

